
package com.zdkj.subway.st.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.st.dto.in.SwStPlanYearDTO;
import com.zdkj.subway.st.entity.SwStPlanMonth;
import com.zdkj.subway.st.entity.SwStPlanYear;
import com.zdkj.subway.st.mapper.SwStPlanYearMapper;
import com.zdkj.subway.st.service.SwStPlanMonthService;
import com.zdkj.subway.st.service.SwStPlanYearService;
import com.zdkj.subway.st.util.GenSerialNo;
import com.zdkj.subway.st.util.SubwayStConstants;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


@Service
@AllArgsConstructor
public class SwStPlanYearServiceImpl extends ServiceImpl<SwStPlanYearMapper, SwStPlanYear> implements SwStPlanYearService {

    private final SwStPlanMonthService swStPlanMonthService;

    @Autowired
    private GenSerialNo genSerialNo;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveWithMonthPlan(SwStPlanYearDTO swStPlanYearDTO) {
        String Z = "Z";//正式计划标识
        SwStPlanYear swStPlanYear = ObjectUtils.getDefaultObj(SwStPlanYear.class);
        BeanUtils.copyProperties(swStPlanYearDTO, swStPlanYear);
        swStPlanYear.setSubmitStatus(SubwayStConstants.SUBMIT_STATUS_WAIT);
        swStPlanYear.setCheckStatus(SubwayStConstants.AUDIT_STATUS_NOT_SUBMIT);
        getBaseMapper().insert(swStPlanYear);
        List<SwStPlanMonth> planMonths = swStPlanYearDTO.getPlanMonths();
        List<SwStPlanMonth> planMonthFilter = planMonths.stream().filter(x -> x.getPlanNumberOld()!=null).collect(Collectors.toList());
        for (SwStPlanMonth planMonth : planMonthFilter) {
            planMonth.setPlanYearId(swStPlanYear.getId());
            planMonth.setDisplay(false);
            planMonth.setWorkTime(swStPlanYear.getPlanYear());
            planMonth.setTicketType(swStPlanYear.getTicketType());
            planMonth.setPlanType(swStPlanYear.getPlanType());
            planMonth.setPlanNumberNow(planMonth.getPlanNumberOld());
            planMonth.setOperator(swStPlanYear.getOperator());
            //生成单号
            //--正式计划
            String generateNum = genSerialNo.generateNewNum(Z + swStPlanYear.getPlanType());
            planMonth.setNumbers(generateNum);
        }
        swStPlanMonthService.saveBatchMonthPlan(planMonthFilter);
        return true;
    }

    @Override
    public boolean updateWithMonthPlan(SwStPlanYearDTO swStPlanYearDTO) {
        SwStPlanYear swStPlanYear = new SwStPlanYear();
        BeanUtils.copyProperties(swStPlanYearDTO, swStPlanYear);
        getBaseMapper().updateById(swStPlanYear);
        List<SwStPlanMonth> planMonths = swStPlanYearDTO.getPlanMonths();
        swStPlanMonthService.remove(Wrappers.<SwStPlanMonth>lambdaQuery()
                .eq(SwStPlanMonth::getPlanYearId, swStPlanYear.getId()));
        List<SwStPlanMonth> planMonthFilter = planMonths.stream().filter(x -> x.getPlanNumberOld()!=null).collect(Collectors.toList());
        for (SwStPlanMonth planMonth : planMonthFilter) {
            planMonth.setPlanYearId(swStPlanYear.getId());
            planMonth.setDisplay(false);
            planMonth.setWorkTime(swStPlanYear.getPlanYear());
            planMonth.setTicketType(swStPlanYear.getTicketType());
            planMonth.setPlanType(swStPlanYear.getPlanType());
            planMonth.setPlanNumberNow(planMonth.getPlanNumberOld());
        }
        swStPlanMonthService.saveBatchMonthPlan(planMonthFilter);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean audit(SwStPlanYear swStPlanYear) {
        PigxUser user = SecurityUtils.getUser();
        swStPlanYear.setCheckPersonId(user.getId());
        swStPlanYear.setCheckTime(LocalDateTime.now());
        if(SubwayStConstants.AUDIT_STATUS_PASS.equals(swStPlanYear.getCheckStatus())){
            swStPlanMonthService.update(Wrappers.<SwStPlanMonth>lambdaUpdate()
                    .eq(SwStPlanMonth::getWorkTime, swStPlanYear.getPlanYear())
                    .set(SwStPlanMonth::getDisplay, true));
        }
        SwStPlanYear updatePlanYear = new SwStPlanYear();
        updatePlanYear.setId(swStPlanYear.getId());
        updatePlanYear.setCheckPersonId(user.getId());
        updatePlanYear.setCheckTime(LocalDateTime.now());
        updatePlanYear.setCheckStatus(swStPlanYear.getCheckStatus());
        updatePlanYear.setCheckSuggest(swStPlanYear.getCheckSuggest());
        return updateById(updatePlanYear);
    }
}
